﻿<!--#include file="include/conn_f.asp"-->
<!--#include file="ipcountry/ipcountry.asp" -->
<!--#include file="top.asp"-->
<!--#include file="left.asp"-->
<br />
<center>
    积分换礼品活动<br />
    <%
If Session("cust_id")="" Then
    Response.Write "请先登录!"
    Response.End
Else
    cust_id=Session("cust_id")
End If
bonus_card_id=cint(request("bonus_card_id"))
amount=cint(request("amount"))
If amount<1 Then
    Response.End
End If
If bonus_card_id=0 Then
    Response.Write "您还没有选择礼品!"
    Response.End
End If
sql="select available_bonus,isactive from customer where cust_id="&cust_id
Set rs_cust=conn.execute(sql)
If Not rs_cust.eof Then
    available_bonus=rs_cust("available_bonus")
    isactive=rs_cust("isactive")
End If
If isactive=2 or isactive=3 Then
    Response.Write "帐号状态异常,无法提取礼品,请联系工作人员!"
    Response.End
End If

sql="select * from bonus_card  where status=1 and bonus_card_id="&bonus_card_id
Set rs=conn.execute(sql)

If Not rs.eof	Then
    card_id=rs("card_id")
    goods_type=rs("goods_type")
    bonus_per=rs("bonus_per")
    amount_left=rs("amount_left")
    limit_per_account=rs("limit_per_account")
    bonus_debit=bonus_per*amount
    SN=rs("SN")
Else
    Response.Write "此卡不可用积分兑换!"
    Response.End
End If
If available_bonus<bonus_per*amount	Then
    Response.Write "<font color='red'>积分不够兑换此数量礼品!请更改数量.</font>"
    Response.End
End If
If amount_left<amount Then
    Response.Write "<font color='red'>剩余的礼品数量不足!请更改数量.</font>"
    Response.End
End If

sql="select sum(amount) from bonus_log where cust_id="&cust_id&" and SN="&SN&" and bonus_card_id="&bonus_card_id
Set rs=conn.execute(sql)
If isnull(rs(0)) Then
    amount_used=0
Else
    amount_used=cint(rs(0))
End If
If amount_used+amount>limit_per_account Then
    Response.Write "<font color='red'>超过该商品单个客户可兑换数量,每客户只可以领取"&limit_per_account&"件!<br />"
    If amount_used>0 Then
        Response.Write "您之前已经兑换过数量:"&amount_used
    End If
    Response.End
End If
IP=Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If IP="" Then
    IP=Request.ServerVariables("remote_addr")
End If
ip_address=split(IP,",",-1,1)
If UBound(ip_address)>-1 and len(ip_address(0))>7 Then
    ipcountry=IP2Country(ip_address(0))
Else
    ipcountry="CN"
End If

sql="select a.price,a.miane,b.card_name from card as a left join card_infor as b on a.card_infor_id=b.card_info_id where card_id="&card_id
Set rs_card=conn.execute(sql)
If Not rs_card.eof Then
    price=rs_card("price")
    miane=rs_card("miane")
    card_name=rs_card("card_name")
End If
total_amount=price*amount
sql="insert into orders(cust_id,goods_type,card_1,card_1_amount,total_amount,order_time,IP,IPCOUNTRY)"
sql=sql&" values("&cust_id&","&goods_type&","&card_id&","&amount&","&total_amount&",now(),'"&IP&"','"&ipcountry&"')"
conn.execute sql
sql="select LAST_INSERT_ID() from orders"
Set rs_temp=conn.execute(sql)
orders_id=rs_temp(0)
rs_temp.close
Session("itemnumber")=orders_id

sql="insert into orders_detail(orders_id,goods_type,card_id,amount) values("&orders_id&","&goods_type&","&card_id&","&amount&")"
conn.execute sql
sql="select LAST_INSERT_ID() from orders_detail"
rs_temp.open sql,conn
order_detail_id=rs_temp(0)
rs_temp.close

If available_bonus<bonus_per*amount	Then
    Response.Write "积分不够兑换此数量礼品!请更改数量."
    Response.End
End If
sql="update customer set available_bonus=available_bonus-"&bonus_debit&" where cust_id="&cust_id
conn.execute sql
sql="insert into bonus_log(cust_id,orders_id,bonus_card_id,amount,bouns_debit,available_bonus,debit_time,SN) values("
sql=sql&cust_id&","&orders_id&","&bonus_card_id&","&amount&","&bonus_debit&","&available_bonus&",now(),"&SN&")"
conn.execute sql
sql="update orders set pay_method=6,pay_status=1,very_code=MD5(orders_id+1+432.48) where orders_id="&orders_id
conn.execute sql
sql="update bonus_card set amount_left=amount_left-"&amount&" where bonus_card_id="&bonus_card_id
conn.execute sql

    
    %>
    <font color="green">积分兑换成功!</font><br />
    <table align="center" width="400" border="1" cellspacing="0" class="unnamed1">
        <caption>
            本次积分兑换信息
            <tr>
                <td>
                    商品信息
                </td>
                <td>
                    <%=card_name&"-"&miane%>
                </td>
            </tr>
            <tr>
                <td>
                    选购数量
                </td>
                <td>
                    <%=amount%>
                </td>
            </tr>
            <tr>
                <td>
                    每件礼品积分
                </td>
                <td>
                    <%=bonus_per%>
                </td>
            </tr>
            <tr>
                <td>
                    本次消耗积分
                </td>
                <td>
                    <%=bonus_debit%>
                </td>
            </tr>
            <tr>
                <td>
                    您的剩余积分
                </td>
                <td>
                    <%=available_bonus-bonus_debit%>
                </td>
            </tr>
    </table>
    <br />
    <br />
    <a href='processing.asp'>自助提货&gt;&gt;</a>
    
    <!--#include file="bottom.asp"-->
